package cn.com.darly.videoandroidpm.common;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LevelListDrawable;
import android.text.Html;
import android.view.View;
import android.widget.TextView;

import com.darly.common.DLog;
import com.darly.dview.common.SCfg;
import com.darly.dview.widget.camera.util.ImageLoaderUtil;
import com.nostra13.universalimageloader.core.assist.FailReason;

/**
 * @author Darly/张宇辉/2018/5/3 17:14
 * @version 1.0/cn.com.darly.videoandroidpm.common
 */
public class HTMLImageGetter implements Html.ImageGetter {

    Context context;
    TextView container;

    public HTMLImageGetter(Context context, TextView container) {
        this.context = context;
        this.container = container;
    }

    @Override
    public Drawable getDrawable(String source) {
        final LevelListDrawable drawable = new LevelListDrawable();
        DLog.i(source);
        ImageLoaderUtil.getInstance().load(source, new ImageLoaderUtil.Loading() {
            @Override
            public void onStarted(String arg0, View arg1) {
                BitmapDrawable bitmap = (BitmapDrawable) context.getResources().getDrawable(com.darly.dview.R.mipmap.empty_photo);
                if(bitmap != null) {
                    drawable.addLevel(1, 1, bitmap);
                    drawable.setBounds(0, 0, SCfg.getWidth()/2,SCfg.getWidth()/2);
                    drawable.setLevel(1);
                    container.invalidate();
                    container.setText(container.getText());
                }
                DLog.i(SCfg.getWidth()/2+"onStarted");
            }

            @Override
            public void onFailed(String arg0, View arg1, FailReason arg2) {
                BitmapDrawable bitmap = (BitmapDrawable) context.getResources().getDrawable(com.darly.dview.R.mipmap.empty_photo);
                if(bitmap != null) {
                    drawable.addLevel(1, 1, bitmap);
                    drawable.setBounds(0, 0, SCfg.getWidth()/2,SCfg.getWidth()/2);
                    drawable.setLevel(1);
                    container.invalidate();
                    container.setText(container.getText());
                }
                DLog.i(SCfg.getWidth()/2+"onFailed");
            }

            @Override
            public void onComplete(String arg0, View arg1, Bitmap bitmap) {
                if(bitmap != null) {
                    BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
                    drawable.addLevel(1, 1, bitmapDrawable);
                    drawable.setBounds(0, 0, SCfg.getWidth()/2,SCfg.getWidth()/2);
                    drawable.setLevel(1);
                    container.invalidate();
                    container.setText(container.getText());
                }
            }

            @Override
            public void onCancelled(String arg0, View arg1) {
                BitmapDrawable bitmap = (BitmapDrawable) context.getResources().getDrawable(com.darly.dview.R.mipmap.empty_photo);
                if(bitmap != null) {
                    drawable.addLevel(1, 1, bitmap);
                    drawable.setBounds(0, 0, SCfg.getWidth()/2,SCfg.getWidth()/2);
                    drawable.setLevel(1);
                    container.invalidate();
                    container.setText(container.getText());
                }
                DLog.i(SCfg.getWidth()/2+"onCancelled");
            }
        });
        return drawable;
    }
}
